/**
 * Copyright(c) 2013 Jade Techonologies Co., Ltd.
 *
 * History:
 *   14-7-29 下午4:54 Created by dwzhang
 */
package com.jade.framework.base.comm.impl;

import com.jade.framework.base.comm.*;
import com.jade.framework.base.net.windowing.WindowFuture;

/**
 * TODO: Must write comments.
 *
 * @author dwzhang
 * @version 1.0 14-7-29 下午4:54
 */
public class WindowResponseFuture implements ResponseFuture
{
    WindowFuture<String, RequestMessage, ResponseMessage> future;

    public WindowResponseFuture(WindowFuture<String, RequestMessage, ResponseMessage> future)
    {
        this.future = future;
    }

    public ResponseMessage get()
            throws CommException
    {
        try {
            boolean result = future.await();
            if (result) {
                return future.getResponse();
            }
            else {
                throw new CommTimeoutException("It's time out to get resposne");
            }
        }
        catch (Throwable t) {
            throw new CommException("It's failed to await");
        }
    }

    public ResponseMessage get(int timeoutInMillis)
            throws CommException, InterruptedException
    {
        boolean result = future.await(timeoutInMillis);
        if (!result) {
            throw new CommTimeoutException("It's time out to get resposne");
        }
        else {
            return future.getResponse();
        }
    }

    public void setCallback(ResponseCallback callback)
    {
        // do nothing
    }

    public boolean isDone()
    {
        return future.isDone();
    }
}
